package com.shangxia.dreaming.common.util.txtUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shangxia.dreaming.business.pojo.TempData;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.shangxia.dreaming.common.content.ConditionContent.*;

/**
 * @ProjectName: dreaming
 * @ClassName: WriteTXT
 * @Time: 2020/10/5
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class WriteTXT {
    public static void saveStr(String fileName, int row) throws IOException {
        File file = new File(PATH_Result + fileName + ".txt");
        if (file.exists()) {
            file.delete();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(PATH_Result + fileName + ".txt")));
        bw.write(row + "");
        bw.close();
    }

    public static void saveMap_int_List(Map<Integer, List<TempData>> map, String fileName) throws IOException {
        if (map == null || map.size() == 0) {
            return;
        }
        File file = new File(PATH_Result + fileName + ".txt");
        if (file.exists()) {
            file.delete();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (Map.Entry<Integer, List<TempData>> entry : map.entrySet()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key", entry.getKey());
            jsonObject.put("value", JSON.toJSONString(entry.getValue()));
            bw.write(jsonObject.toJSONString());
            bw.newLine();
        }
        bw.close();
    }

    public static void saveStrList(List<String> list, String fileName) throws IOException {
        if (list == null || list.size() == 0) {
            return;
        }
        File file = new File(PATH_Result + fileName + ".txt");
        if (file.exists()) {
            file.delete();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(PATH_Result + fileName + ".txt")));
        bw.write(JSON.toJSONString(list));
        bw.close();
    }

    public static void saveIntList(List<Integer> list, String fileName) throws IOException {
        if (list == null || list.size() == 0) {
            return;
        }
        File file = new File(PATH_Result + fileName + ".txt");
        if (file.exists()) {
            file.delete();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(PATH_Result + fileName + ".txt")));
        for (int i = 0; i < list.size(); i++) {
            bw.write("第" + (i + 1) + "列：相邻年龄=" + list.get(i));
            bw.newLine();
        }
        bw.close();
    }

    public static void saveTempDataList(List<TempData> list, String fileName) throws IOException {
        if (list == null || list.size() == 0) {
            return;
        }
        File file = new File(PATH_Result + fileName + ".txt");
        if (file.exists()) {
            file.delete();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(PATH_Result + fileName + ".txt")));
        for (TempData tempData : list) {
            bw.write(JSON.toJSONString(tempData));
            bw.newLine();
        }
        bw.close();
    }

    public static void saveLists(Set<List<Integer>> list, String fileName) throws IOException {
        if (list == null || list.size() == 0) {
            return;
        }
        File file = new File(PATH_Result + fileName + ".txt");
        if (file.exists()) {
            file.delete();
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(PATH_Result + fileName + ".txt")));
        for (List<Integer> integers : list) {
            bw.write(JSON.toJSONString(integers));
            bw.newLine();
        }
        bw.close();
    }

}
